package com.luisliuyi.demo.opengl;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.opengl.GLSurfaceView;
import android.os.Environment;

import androidx.camera.core.Preview;
import androidx.lifecycle.LifecycleOwner;

import java.io.File;
import java.io.IOException;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class CameraRender implements GLSurfaceView.Renderer, Preview.OnPreviewOutputUpdateListener, SurfaceTexture.OnFrameAvailableListener {
    private CameraView cameraView;
    private CameraHelper cameraHelper;
    private SurfaceTexture mCameraTexure;
    private int[] textures;
    float[] mtx = new float[16];
    private CameraFilter cameraFilter;
    private SoulFilter soulFilter;
    private SplitFilter splitFilter;
    private RecordFilter recordFilter;

    private MediaRecorder mRecorder;

    public CameraRender(CameraView cameraView) {
        this.cameraView = cameraView;
        LifecycleOwner lifecycleOwner = (LifecycleOwner) cameraView.getContext();
        // 打开摄像头
        cameraHelper = new CameraHelper(lifecycleOwner, this);
    }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        textures = new int[1];
        //让 SurfaceTexture与 Gpu  共享一个数据源
        mCameraTexure.attachToGLContext(textures[0]);
        //监听摄像头数据回调，
        mCameraTexure.setOnFrameAvailableListener(this);
        Context context = cameraView.getContext();
        cameraFilter = new CameraFilter(context);
        soulFilter = new SoulFilter(context);
        splitFilter = new SplitFilter(context);
        recordFilter = new RecordFilter(context);
        File file = new File(Environment.getExternalStorageDirectory(), "input.mp4");
        if (file.exists()) {
            file.delete();
        }
        String path = file.getAbsolutePath();
        mRecorder = new MediaRecorder(cameraView.getContext(), path,
                EGL14.eglGetCurrentContext(),
                480, 640);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        cameraFilter.setSize(width,height);
        soulFilter.setSize(width,height);
        splitFilter.setSize(width,height);
        recordFilter.setSize(width,height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // 摄像头的数据给gpu
        mCameraTexure.updateTexImage();
        mCameraTexure.getTransformMatrix(mtx);
        cameraFilter.setTransformMatrix(mtx);
        int id = cameraFilter.onDraw(textures[0]);
        id = soulFilter.onDraw(id);
        id = recordFilter.onDraw(id);
        mRecorder.fireFrame(id,mCameraTexure.getTimestamp());
    }

    @Override
    public void onUpdated(Preview.PreviewOutput output) {
        //摄像头预览到的数据 在这里
        mCameraTexure=output.getSurfaceTexture();
    }

    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        cameraView.requestRender();
    }

    public void startRecord(float speed) {
        try {
            mRecorder.start(speed);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void stopRecord() {
        mRecorder.stop();
    }
}
